{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Numpy是python科学计算的基础包，它提供以下功能：\n",
    "\n",
    "* 快速高效的多维数组对象ndarray \n",
    "* 用于对数组执行元素级计算以及直接对数组执行数学运算的函数\n",
    "* 用于读写硬盘上基于数组的数据集的工具\n",
    "* 线性代数运算、傅里叶变换，以及随机数生成\n",
    "* 用于将C、C++、Fortran代码集成到python的工具"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[『ipynb文档传送门』](https://gitee.com/iherr/ipynb/blob/master/dataanalysis/numpy.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# python list方法生成数据\n",
    "list(range(0,10,2))\n",
    "#[0, 2, 4, 6, 8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 二维数据访问\n",
    "var =[[1,2,3],[4,5,6],[7,8,9]]\n",
    "var[0][0]\n",
    "#1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 根据数组创建np对象\n",
    "a=np.array(var)\n",
    "a\n",
    "# array([[1, 2, 3],\n",
    "#        [4, 5, 6],\n",
    "#        [7, 8, 9]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# np访问\n",
    "a[0][0],a[0,0]\n",
    "#(1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# np数组切片\n",
    "a[:2,:2]\n",
    "# array([[1, 2],\n",
    "#        [4, 5]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a1=np.arange(15).reshape(3,5)\n",
    "a1\n",
    "# array([[ 0,  1,  2,  3,  4],\n",
    "#        [ 5,  6,  7,  8,  9],\n",
    "#        [10, 11, 12, 13, 14]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数组的dimentsions，对应matrix的rows和columns\n",
    "a1.shape\n",
    "#(3, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数组的维度dimensions数量，numpy也叫axes，线性代数中矩阵的秩\n",
    "a1.ndim\n",
    "#2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元素的type\n",
    "a1.dtype\n",
    "#dtype('int64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元素的type名\n",
    "a1.dtype.name\n",
    "#'int64'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 每个元素的字节数\n",
    "a1.itemsize\n",
    "#8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元素总数\n",
    "a1.size\n",
    "#15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(a1)\n",
    "#numpy.ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b1=np.arange(6)\n",
    "b1\n",
    "#array([0, 1, 2, 3, 4, 5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print(b1)\n",
    "#[0 1 2 3 4 5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、数组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# array创建，参数为数组\n",
    "a2=np.array([6,7,8])\n",
    "a2\n",
    "#array([6, 7, 8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(a2)\n",
    "#numpy.ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a2.dtype\n",
    "#dtype('int64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b2=np.array([(1.5,2,3),(4,5,6)])\n",
    "b2\n",
    "# array([[1.5, 2. , 3. ],\n",
    "#       [4. , 5. , 6. ]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元素中有float\n",
    "b2.dtype\n",
    "#dtype('float64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b2.size\n",
    "#6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b2.itemsize\n",
    "#8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建复数，包括实数和虚数\n",
    "c2=np.array([[1,2],[3,4]],dtype=complex)\n",
    "c2\n",
    "# array([[1.+0.j, 2.+0.j],\n",
    "#        [3.+0.j, 4.+0.j]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 特定方法，参数为shape，一维：2，二维：(3,4)，三维：(2,3,4)\n",
    "d2=np.zeros((3,4),dtype=np.complex)\n",
    "d2\n",
    "# array([[0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
    "#        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],\n",
    "#        [0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d2.itemsize\n",
    "#16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "e2=np.ones((2,3,4),dtype=np.int32)\n",
    "e2\n",
    "# array([[[1, 1, 1, 1],\n",
    "#         [1, 1, 1, 1],\n",
    "#         [1, 1, 1, 1]],\n",
    "\n",
    "#        [[1, 1, 1, 1],\n",
    "#         [1, 1, 1, 1],\n",
    "#         [1, 1, 1, 1]]], dtype=int32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f2=np.full((2,3),3.14)\n",
    "f2\n",
    "# array([[3.14, 3.14, 3.14],\n",
    "#        [3.14, 3.14, 3.14]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g2=np.full_like(f2,3.2,dtype=np.float)\n",
    "g2\n",
    "#array([[3.2, 3.2, 3.2],\n",
    "#        [3.2, 3.2, 3.2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "h2=np.empty((2,3))\n",
    "h2\n",
    "# array([[3.2, 3.2, 3.2],\n",
    "#        [3.2, 3.2, 3.2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# arange函数，设置开始、结束和步长。linspace函数，设置开始、结束和数量\n",
    "j2=np.arange(10,30,5)\n",
    "j2\n",
    "#array([10, 15, 20, 25])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "k2=np.arange(0,2,0.3)\n",
    "k2\n",
    "#array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l2=np.linspace(0,2,9)\n",
    "l2\n",
    "#array([0.  , 0.25, 0.5 , 0.75, 1.  , 1.25, 1.5 , 1.75, 2.  ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n2=np.eye(5)\n",
    "n2\n",
    "# array([[1., 0., 0., 0., 0.],\n",
    "#        [0., 1., 0., 0., 0.],\n",
    "#        [0., 0., 1., 0., 0.],\n",
    "#        [0., 0., 0., 1., 0.],\n",
    "#        [0., 0., 0., 0., 1.]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、基本运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a4=np.array([20,30,40,50])\n",
    "a4\n",
    "#array([20, 30, 40, 50])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b4=np.arange(4)\n",
    "b4\n",
    "#array([0, 1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元素相减\n",
    "c4=a4-b4\n",
    "c4\n",
    "#array([20, 29, 38, 47])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a4+10\n",
    "#array([30, 40, 50, 60])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b4**2\n",
    "#array([0, 1, 4, 9])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "10*np.sin(a4)\n",
    "#array([ 9.12945251, -9.88031624,  7.4511316 , -2.62374854])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.any(a4>30)\n",
    "#True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.all(a4>20)\n",
    "#False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "a4<35\n",
    "#array([ True,  True, False, False])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c4=np.array([[1,1],[0,1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d4=np.array([[2,0],[3,4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 排序\n",
    "e4=np.sort(d4)\n",
    "e4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d4.sort(axis=1)\n",
    "d4\n",
    "#array([[0, 2],\n",
    "#        [3, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵操作\n",
    "A = np.array( [[1,1],[0,1]] )\n",
    "B = np.array( [[2,0],[3,4]] )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元素乘\n",
    "A*B\n",
    "#array([[2, 0],\n",
    "#        [0, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵乘\n",
    "A@B\n",
    "# array([[5, 4],\n",
    "#        [3, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵乘\n",
    "A.dot(B)\n",
    "# array([[5, 4],\n",
    "#        [3, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一维操作 \n",
    "e4=np.random.random((2,3))\n",
    "e4\n",
    "# array([[0.65666429, 0.06776115, 0.59039502],\n",
    "#        [0.29474361, 0.70762869, 0.42369208]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sum(e4)\n",
    "#array([1.2493767 , 0.52770743, 0.85795826])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "e4.sum()\n",
    "#2.635042388410591"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "e4.min()\n",
    "#0.03045008181383413"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "e4.max()\n",
    "#0.9572964175664475"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 轴操作\n",
    "f4=np.arange(12).reshape(3,4)\n",
    "f4\n",
    "# array([[ 0,  1,  2,  3],\n",
    "#        [ 4,  5,  6,  7],\n",
    "#        [ 8,  9, 10, 11]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f4.sum(axis=0)\n",
    "#array([12, 15, 18, 21])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "np.sum(f4)\n",
    "#66"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f4.sum(axis=1)\n",
    "#array([ 6, 22, 38])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "f4.cumsum(axis=1)\n",
    "# array([[ 0,  1,  3,  6],\n",
    "#        [ 4,  9, 15, 22],\n",
    "#        [ 8, 17, 27, 38]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#比较sum和np.sum的执行效率\n",
    "n=np.random.rand(1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%timeit sum(n)\n",
    "#13.6 µs ± 251 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#np执行效率高\n",
    "%timeit np.sum(n)\n",
    "#4.75 µs ± 207 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 四、通用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b=np.arange(3)\n",
    "b\n",
    "#array([0, 1, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.exp(b)\n",
    "#array([1.        , 2.71828183, 7.3890561 ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sqrt(b)\n",
    "#array([0.        , 1.        , 1.41421356])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c=np.array([2,-1,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.add(b,c)\n",
    "#array([2, 0, 6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b+c\n",
    "#array([2, 0, 6])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 五、索引、切片和迭代（Indexing, Slicing and Iterating）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.arange(10)**3\n",
    "a\n",
    "#array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[2]\n",
    "#8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[2:5]\n",
    "#array([ 8, 27, 64])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 等同于a[0:6:2] = -1000;从0到第6个元素，步长为2\n",
    "a[:6:2]\n",
    "#array([ 0,  8, 64])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元素倒序\n",
    "a[::-1]\n",
    "#array([729, 512, 343, 216, 125,  64,  27,   8,   1,   0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in a:\n",
    "    print(i**(1/3.))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y):\n",
    "    return 10*x+y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b=np.fromfunction(f,(5,4),dtype=int)\n",
    "b\n",
    "# array([[ 0,  1,  2,  3],\n",
    "#        [10, 11, 12, 13],\n",
    "#        [20, 21, 22, 23],\n",
    "#        [30, 31, 32, 33],\n",
    "#        [40, 41, 42, 43]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b[2,3]\n",
    "#23"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b[0:5,1]\n",
    "#array([ 1, 11, 21, 31, 41])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b[:,1]\n",
    "#array([ 1, 11, 21, 31, 41])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b[0:2,:]\n",
    "#array([[ 0,  1,  2,  3],\n",
    "#        [10, 11, 12, 13]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b[0:2,0:2]\n",
    "# array([[ 0,  1],\n",
    "#        [10, 11]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b[0:2][0:2]\n",
    "# array([[ 0,  1,  2,  3],\n",
    "#        [10, 11, 12, 13]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#迭代\n",
    "for row in b:\n",
    "    print(row)\n",
    "# [0 1 2 3]\n",
    "# [10 11 12 13]\n",
    "# [20 21 22 23]\n",
    "# [30 31 32 33]\n",
    "# [40 41 42 43]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for ele in b.flat:\n",
    "    print(ele)\n",
    "# 0\n",
    "# 1\n",
    "# 2\n",
    "# 3\n",
    "# 10\n",
    "# 11\n",
    "# 12\n",
    "# 13\n",
    "# ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 六、Array shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])\n",
    "a\n",
    "# array([[ 1,  2,  3,  4],\n",
    "#        [ 5,  6,  7,  8],\n",
    "#        [ 9, 10, 11, 12]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.ravel()\n",
    "#array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 如果为-1，会自动计算，等同于a.reshape(6,2)\n",
    "a.reshape(6,-1)\n",
    "# array([[ 1,  2],\n",
    "#        [ 3,  4],\n",
    "#        [ 5,  6],\n",
    "#        [ 7,  8],\n",
    "#        [ 9, 10],\n",
    "#        [11, 12]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转置\n",
    "a.T\n",
    "# array([[ 1,  5,  9],\n",
    "#        [ 2,  6, 10],\n",
    "#        [ 3,  7, 11],\n",
    "#        [ 4,  8, 12]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# reshape和转置后，a本身没有变化\n",
    "a\n",
    "# array([[ 1,  2,  3,  4],\n",
    "#        [ 5,  6,  7,  8],\n",
    "#        [ 9, 10, 11, 12]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# reshape返回新的array，resize修改array自身。如需要修改自身，也可直接使用shape属性，等同于a.shape=(6,2)\n",
    "a.resize(6,2)\n",
    "a\n",
    "# array([[ 1,  2],\n",
    "#        [ 3,  4],\n",
    "#        [ 5,  6],\n",
    "#        [ 7,  8],\n",
    "#        [ 9, 10],\n",
    "#        [11, 12]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 七、Array stack together"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([[1,2],[3,4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b=np.array([[5,6],[7,8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.concatenate((a,b),axis=0)\n",
    "# array([[1, 2],\n",
    "#        [3, 4],\n",
    "#        [5, 6],\n",
    "#        [7, 8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.concatenate((a,b),axis=1)\n",
    "# array([[1, 2, 5, 6],\n",
    "#        [3, 4, 7, 8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "np.vstack((a,b))\n",
    "# array([[1, 2],\n",
    "#        [3, 4],\n",
    "#        [5, 6],\n",
    "#        [7, 8]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.hstack((a,b))\n",
    "# array([[1, 2, 5, 6],\n",
    "#        [3, 4, 7, 8]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 八、Array splitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.array([[1,2,3,4,5,6,7,8,9,10,11,12],[21,22,23,24,25,26,27,28,29,30,31,32]])\n",
    "a\n",
    "# array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12],\n",
    "#        [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 按列3等分\n",
    "np.hsplit(a,3)\n",
    "# [array([[ 1,  2,  3,  4],\n",
    "#         [21, 22, 23, 24]]), array([[ 5,  6,  7,  8],\n",
    "#         [25, 26, 27, 28]]), array([[ 9, 10, 11, 12],\n",
    "#         [29, 30, 31, 32]])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 从第三和第四列分割\n",
    "np.hsplit(a,(3,4))\n",
    "# [array([[ 1,  2,  3],\n",
    "#         [21, 22, 23]]), array([[ 4],\n",
    "#         [24]]), array([[ 5,  6,  7,  8,  9, 10, 11, 12],\n",
    "#         [25, 26, 27, 28, 29, 30, 31, 32]])]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 九、Copies and Views"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、直接复制，引用指向相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.arange(12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b=a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b is a\n",
    "#True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.shape= (3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.shape\n",
    "#(3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、view，变更shape不影响，修改值会影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c=a.view()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c is a \n",
    "#False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c.base is a \n",
    "#True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c.shape=(2,6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.shape\n",
    "#(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c[0,4]=1234"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a\n",
    "# array([[   0,    1,    2,    3],\n",
    "#        [1234,    5,    6,    7],\n",
    "#        [   8,    9,   10,   11]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、copy，变更shape和修改值都不会影响"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d=a.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d is a \n",
    "#False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d.base is a\n",
    "#False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d[0,0]=9999\n",
    "d\n",
    "# array([[9999,    1,    2,    3],\n",
    "#        [1234,    5,    6,    7],\n",
    "#        [   8,    9,   10,   11]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a\n",
    "# array([[   0,    1,    2,    3],\n",
    "#        [1234,    5,    6,    7],\n",
    "#        [   8,    9,   10,   11]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 十、Indexing with Arrays of Indices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "a=np.arange(12)**2\n",
    "a\n",
    "#array([  0,   1,   4,   9,  16,  25,  36,  49,  64,  81, 100, 121])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i=np.array([1,1,3,5,8])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[i]\n",
    "#array([ 1,  1,  9, 25, 64])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "j=np.array([[3,4],[5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[j]\n",
    "# array([[ 9, 16],\n",
    "#        [25, 36]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.arange(12).reshape(3,4)\n",
    "a\n",
    "# array([[ 0,  1,  2,  3],\n",
    "#        [ 4,  5,  6,  7],\n",
    "#        [ 8,  9, 10, 11]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i=np.array([[0,1],[1,2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "j=np.array([[2,1],[3,3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[i,j]\n",
    "# array([[ 2,  5],\n",
    "#        [ 7, 11]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[i,2]\n",
    "# array([[ 2,  6],\n",
    "#        [ 6, 10]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.arange(5)\n",
    "a\n",
    "#array([0, 1, 2, 3, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[[0,0,2]]=[1,2,3]\n",
    "a\n",
    "#array([2, 1, 3, 3, 4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 十一、Random 随机"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 0、random模块的随机方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#生成[5,10]之间的随机整数\n",
    "random.randint(5,10)\n",
    "#7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#生成0-1的float数据\n",
    "random.random()\n",
    "#0.6050909097237349"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 以下为np模块的random随机方法\n",
    "### 1、numpy.random.rand(d0,d1,…,dn)\n",
    "\n",
    "* rand函数根据给定维度生成0到1，前闭后开区间的数据，包含0，不包含1\n",
    "* dn表格每个维度\n",
    "* 返回值为指定维度的array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.rand(3,3)\n",
    "# array([[0.66623244, 0.45705496, 0.58081625],\n",
    "#        [0.13670723, 0.78440093, 0.7079095 ],\n",
    "#        [0.40285142, 0.70139829, 0.82308891]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、numpy.random.randn(d0,d1,…,dn)\n",
    "\n",
    "* randn函数返回一个或一组样本，具有标准正态分布(standard normal distribution)。标准正态分布又称为u分布，是以0为均值、以1为标准差的正态分布，记为N（0，1）。\n",
    "* dn表格每个维度，当没有参数时，返回单个数据\n",
    "* 返回值为指定维度的array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.randn() \n",
    "#0.6423112533434442"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.randn(3,3) \n",
    "# array([[ 0.09658991, -2.83590735, -0.21460649],\n",
    "#        [-1.77854159, -0.82415655, -0.4636123 ],\n",
    "#        [ 0.58119767, -0.57786413,  0.0742295 ]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、numpy.random.randint(low, high=None, size=None, dtype=’l’)\n",
    "\n",
    "* 返回随机整数，范围区间为[low,high），包含low，不包含high\n",
    "* 参数：low为最小值，high为最大值，size为数组维度大小，dtype为数据类型，默认的数据类型是np.int\n",
    "* high没有填写时，默认生成随机数的范围是[0，low)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回[0,1)之间的整数，所以只有0\n",
    "np.random.randint(1,size=5)\n",
    "#array([0, 0, 0, 0, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 返回1个[1,5)时间的随机整数\n",
    "np.random.randint(1,5) \n",
    "#2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成区间为[0,10)的随机整数\n",
    "np.random.randint(0,10,size=(2,2))\n",
    "# array([[1, 1],\n",
    "#        [6, 8]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、浮点数\n",
    "\n",
    "生成[0,1)之间的浮点数\n",
    "\n",
    "* numpy.random.random_sample(size=None)\n",
    "* numpy.random.random(size=None)\n",
    "* numpy.random.ranf(size=None)\n",
    "* numpy.random.sample(size=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('--random_sample--')\n",
    "print(np.random.random_sample(size=(2,2)))\n",
    "print('--random--')\n",
    "print(np.random.random(size=(2,2)))\n",
    "print('--ranf--')\n",
    "print(np.random.ranf(size=(2,2)))\n",
    "print('--sample--')\n",
    "print(np.random.sample(size=(2,2)))\n",
    "# --random_sample--\n",
    "# [[0.81408113 0.09255191]\n",
    "#  [0.37559088 0.77663326]]\n",
    "# --random--\n",
    "# [[0.83663939 0.66350458]\n",
    "#  [0.99431816 0.23499516]]\n",
    "# --ranf--\n",
    "# [[0.4313313  0.63957712]\n",
    "#  [0.25859169 0.74576795]]\n",
    "# --sample--\n",
    "# [[0.10410069 0.02092188]\n",
    "#  [0.07719955 0.87703089]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5、numpy.random.choice(a, size=None, replace=True, p=None)\n",
    "\n",
    "* 从给定的一维数组中生成随机数\n",
    "* 参数：a为一维数组类似数据或整数；size为数组维度；p为数组中的数据出现的概率\n",
    "* a为整数时，对应的一维数组为np.arange(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.choice(5,3)\n",
    "#array([3, 1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 当replace为False时，生成的随机数不能有重复的数值\n",
    "np.random.choice(5, 3, replace=False)\n",
    "#array([4, 0, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.choice(5,size=(3,2))\n",
    "# array([[3, 4],\n",
    "#        [1, 1],\n",
    "#        [4, 2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']\n",
    "np.random.choice(demo_list,size=(3,3))\n",
    "# array([['moto', 'lenovo', 'moto'],\n",
    "#        ['lenovo', 'sansumg', 'iphone'],\n",
    "#        ['moto', 'sansumg', 'xiaomi']], dtype='<U7')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#参数p的长度与参数a的长度需要一致；\n",
    "#参数p为概率，p里的数据之和应为1\n",
    "demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']\n",
    "np.random.choice(demo_list,size=(3,3), p=[0.1,0.6,0.1,0.1,0.1])\n",
    "# array([['xiaomi', 'sansumg', 'lenovo'],\n",
    "#        ['sansumg', 'sansumg', 'lenovo'],\n",
    "#        ['sansumg', 'sansumg', 'lenovo']], dtype='<U7')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6、numpy.random.seed()\n",
    "\n",
    "* 使得随机数据可预测。\n",
    "* 当我们设置相同的seed，每次生成的随机数相同。如果不设置seed，则每次会生成不同的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(0)\n",
    "np.random.rand(5)\n",
    "#array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(1676)\n",
    "np.random.rand(5)\n",
    "#array([0.39983389, 0.29426895, 0.89541728, 0.71807369, 0.3531823 ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(1676)\n",
    "np.random.rand(5)\n",
    "#array([0.39983389, 0.29426895, 0.89541728, 0.71807369, 0.3531823 ])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
